package com.service;

import com.dao.MemberDao;
import com.dto.BookDTO;
import com.dto.MemberDTO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.po.MemberPO;
import com.util.RestException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author linyukun
 * @date 2022/7/12 18:18
 */
@Service
public class MemberServiceImpl {
    @Autowired
    private MemberDao memberDao;
    @Value("${max.user.borrow.num}")
    private String maxUserBorrowNum;

    public Object addMember(MemberDTO dto) {
        this.checkAddAndEditMemberInfo(dto);
        MemberPO idCardMember = memberDao.getMemberByIdCard(dto.getIdCard());
        if(!ObjectUtils.isEmpty(idCardMember)){
            throw new RestException("该身份证已经被使用了");
        }
        MemberPO phoneNumber = memberDao.getMemberByphoneNumber(dto.getPhoneNumber());
        if(!ObjectUtils.isEmpty(phoneNumber)){
            throw new RestException("该手机号已经被注册了");
        }
        //判断这个身份证号是否已经存在了
        dto.setBorrowNum(new Integer(maxUserBorrowNum));
        return memberDao.addMember(dto);
    }

    public Object editMember(MemberDTO dto) {
        if(ObjectUtils.isEmpty(dto.getId())){
            throw new RestException("请传递参数");
        }
        this.checkAddAndEditMemberInfo(dto);
        MemberPO idCardMember = memberDao.getMemberByIdCard(dto.getIdCard());
        if(!ObjectUtils.isEmpty(idCardMember) && !idCardMember.getId().equals(dto.getId())){
            throw new RestException("该身份证已经被使用了");
        }
        MemberPO phoneNumber = memberDao.getMemberByphoneNumber(dto.getPhoneNumber());
        if(!ObjectUtils.isEmpty(phoneNumber) && !idCardMember.getId().equals(dto.getId())){
            throw new RestException("该手机号已经被注册了");
        }
        return memberDao.editMember(dto);
    }


    /**
     * 校验添加会员或者修改员的信息的方法
     * @author linyukn
     * @date 2022-07-09 17:27
     */
    public void checkAddAndEditMemberInfo(MemberDTO dto){
        if(StringUtils.isBlank(dto.getIdCard())){
            throw new RestException("请传递身份证");
        }
        if(StringUtils.isBlank(dto.getPhoneNumber())){
            throw new RestException("请传递联系电话");
        }
        if(StringUtils.isBlank(dto.getPassword())){
            throw new RestException("请传递密码");
        }
//        if(StringUtils.isBlank(dto.get())){
//            throw new RestException("请传递用户名称");
//        }
    }

    public Object getMemberById(MemberDTO dto) {
        return memberDao.getMemberById(dto.getId());
    }

    public Object getMemberList(MemberDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new PageInfo(memberDao.getMemberList(dto));
    }


    public Object getAdminMemberList(MemberDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new PageInfo(memberDao.getAdminMemberList(dto));
    }

    public Object delMember(MemberDTO dto) {
        return memberDao.delMember(dto);
    }

    public Object addAdmin(MemberDTO dto) {
        if(StringUtils.isBlank(dto.getUserName())){
            throw new RestException("请传递账号");
        }
        if(StringUtils.isBlank(dto.getPassword())){
            throw new RestException("请传递密码");
        }
        List<MemberPO> dbMembers = memberDao.getAdminByUserName(dto.getUserName());
        if(!CollectionUtils.isEmpty(dbMembers)){
            throw new RestException("该用户已经存在了");
        }
       return memberDao.addAdmin(dto);
    }

    public Object resetAdminPassword(MemberDTO dto) {
        return memberDao.resetAdminPassword(dto);
    }
}
